package com.sit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.sit.entity.CompetitionT;

import com.sit.mapper.CompetitionTMapper;

import com.sit.service.CompetitionTService;

import com.sit.vo.CompetitionTVO;
import com.sit.vo.DataVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CompetitionTServiceImpl implements CompetitionTService {

    @Autowired
    private CompetitionTMapper competitionTMapper;
    @Override
    public DataVO<CompetitionTVO> findData() {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");
        dataVO.setCount(competitionTMapper.selectCount(null).longValue());
        List<CompetitionT> competitionTList = competitionTMapper.selectList(null);
        dataVO.setData(competitionTList);
        return dataVO;
    }

    @Override
    public boolean addComp(CompetitionT competitionT) {
        CompetitionT competitionT1 = new CompetitionT();
        competitionT1.setCnA(competitionT.getCnA());
        competitionT1.setCnoA(competitionT.getCnoA());
        competitionT1.setMax(competitionT.getMax());
        competitionT1.setPlaceA(competitionT.getPlaceA());
        competitionT1.setDateA(competitionT.getDateA());
        int flag1 =competitionTMapper.insert(competitionT1);

        if(flag1 >0){
            return  true;
        }else {return false;}
    }

    @Override
    public boolean deleteComp(String cnoA) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("cno_a",cnoA);
        int flag =competitionTMapper.delete(wrapper);
        if(flag >0){
            return  true;
        }else {return false;}
    }

    @Override
    public boolean updateComp(CompetitionT competitionT) {
        CompetitionT competitionT1 = new CompetitionT();
        BeanUtils.copyProperties(competitionT,competitionT1);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("cno_a",competitionT.getCnoA());
        int flag =competitionTMapper.update(competitionT1,wrapper);
        if(flag >0){
            return  true;
        }else {return false;}
    }

    @Override
    public DataVO<CompetitionTVO> searchComp(String cnA) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("cn_a",cnA);
        dataVO.setCount(competitionTMapper.selectCount(wrapper).longValue());

        CompetitionTVO competitionTVO = new CompetitionTVO();
        BeanUtils.copyProperties(competitionTMapper.selectOne(wrapper),competitionTVO);
        List<CompetitionTVO> competitionTVOList = new ArrayList<>();
        competitionTVOList.add(competitionTVO);
        dataVO.setData(competitionTVOList);
        return dataVO;
    }
}
